package com.baor.service.impl;

import com.baor.dao.MenuDao;
import com.baor.dao.RoleDao;
import com.baor.domain.AdminVo;
import com.baor.domain.RoleVo;
import com.baor.service.AdminService;
import com.baor.service.RoleService;
import com.baor.utils.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.kafka.KafkaProperties;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;
@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private AdminService adminService;
    @Autowired
    private MenuDao menuDao;
    @Autowired
    private AdminServiceImpl adminServiceImpl;
    @Override
    public AjaxResult<?> list(Map<String, Object> params) {
        int pageIndex = (int) params.get("pageIndex");
        int pageSize = (int) params.get("pageSize");
        params.put("pageIndex",(pageIndex - 1) * pageSize);
        Map<String,Object> res = roleDao.findPageCount(params);
        List<RoleVo> list = this.roleDao.roleList(params);
        res.put("list",list);
        return AjaxResult.success(res);
    }


    @Override
    public AjaxResult<?> delete(Map<String,Object> params) {
        int roleId = (int) params.get("roleId");

        if(roleId == 0){
            AjaxResult.error("不能删除超管");
        }
        int row = this.roleDao.deleteById(roleId);
        if(row>0){
            return AjaxResult.success();
        }else{
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult<?> insert(Map<String,Object> params) {
//        AdminVo adminVo = adminServiceImpl.getVo();
//        System.out.println(adminVo);
//        params.put("siteId",adminVo.getSiteId());
        if(!params.containsKey("createTime")){
            LocalDateTime currentDateTime = LocalDateTime.now();
            DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String formattedDateTime = currentDateTime.format(formatter);
            params.put("createTime",formattedDateTime);
        }
        int row = roleDao.insert(params);
        int roleId = roleDao.findRoleId();
        if(params.containsKey("menuId")){
            List<Integer> menuId = (List<Integer>) params.get("menuId");
            for(int i = 0;i<menuId.size();i++){
                int menuId1 = menuId.get(i);
                params.put("menuId",menuId1);
                params.put("roleId",roleId);
                int add = this.menuDao.addRoleMenu(params);
            }
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult<?> update(Map<String, Object> params) {
        if(params.containsKey("menuId")){
            int delete = menuDao.delete(params);
            List<Integer> menuId = (List<Integer>) params.get("menuId");
            for(int i = 0;i<menuId.size();i++){
                int menuId1 = menuId.get(i);
                params.put("menuId",menuId1);
                int add = this.menuDao.addRoleMenu(params);
            }
        }
        int row = roleDao.update(params);
        return AjaxResult.success();
    }

    @Override
    public AjaxResult<?> selectRole() {
        return AjaxResult.success(this.roleDao.selectList(null));
    }
}
